using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace WorkFlow.Tools.UI
{
    [ExecuteAlways]
    [RequireComponent(typeof(UIBehaviourDriver))]
    [AddComponentMenu("WorkFlow/UGUI/UIBehaviour/VerticalRatio")]
    public class VerticalRatio : MonoBehaviour,IUIBehaviourDriverElement
    {
        [SerializeField] private int order;
        [SerializeField] private bool useTopRatio;

        [SerializeField] [ShowIf("useTopRatio")]
        private float topRatio;

        [SerializeField] private bool useBottomRatio;

        [SerializeField] [ShowIf("useBottomRatio")]
        private float bottomRatio;

        [SerializeField] [ShowIf("@!(useTopRatio && useBottomRatio)")]
        private float fixedHeight;

        [SerializeField] [ShowIf("@!(useTopRatio && useBottomRatio)")] [ReadOnly]
        private float anotherRatio;

        private RectTransform _rectTransform;

        RectTransform URectTransform
        {
            get
            {
                if (_rectTransform == null)
                    _rectTransform = GetComponent<RectTransform>();
                return _rectTransform;
            }
        }
        bool IUIBehaviourDriverElement.ElementEnabled => enabled;
        int IUIBehaviourDriverElement.Order => order;
        void IUIBehaviourDriverElement.OnRectTransformDimensionsChange()
        {
            ApplyRatio();
        }

        void ApplyRatio()
        {
#if UNITY_EDITOR
                _isValidate = false;
#endif
            if (!enabled) return;
            if (useTopRatio && useBottomRatio)
            {
                URectTransform.anchorMin = new Vector2(URectTransform.anchorMin.x, bottomRatio);
                URectTransform.anchorMax = new Vector2(URectTransform.anchorMax.x, 1 - topRatio);
                URectTransform.pivot = new Vector2(URectTransform.pivot.x, 0.5f);
                URectTransform.offsetMax = new Vector2(URectTransform.offsetMax.x, 0);
                URectTransform.offsetMin = new Vector2(URectTransform.offsetMin.x, 0);
            }
            else if (useTopRatio)
            {
                URectTransform.anchorMax = new Vector2(URectTransform.anchorMax.x, 1 - topRatio);
                URectTransform.anchorMin = new Vector2(URectTransform.anchorMin.x, 1 - topRatio);
                URectTransform.pivot = new Vector2(URectTransform.pivot.x, 1f);
                URectTransform.offsetMax = new Vector2(URectTransform.offsetMax.x, 0);
                URectTransform.offsetMin = new Vector2(URectTransform.offsetMin.x, -fixedHeight);
#if UNITY_EDITOR
                anotherRatio = 1 - fixedHeight / transform.parent.GetComponent<RectTransform>().rect.height;
#endif
            }
            else if (useBottomRatio)
            {
                URectTransform.anchorMax = new Vector2(URectTransform.anchorMax.x, topRatio);
                URectTransform.anchorMin = new Vector2(URectTransform.anchorMin.x, bottomRatio);
                URectTransform.pivot = new Vector2(URectTransform.pivot.x, 0f);
                URectTransform.offsetMax = new Vector2(URectTransform.offsetMax.x, fixedHeight);
                URectTransform.offsetMin = new Vector2(URectTransform.offsetMin.x, 0);
            }
        }

#if UNITY_EDITOR
        [Button]
        void SetNativeSize()
        {
            fixedHeight = LayoutUtility.GetPreferredHeight(URectTransform);
        }
        private bool _isValidate;
        bool IUIBehaviourDriverElement.IsValidated => _isValidate;
        void OnValidate()
        {
            _isValidate = true;
        }
#endif
    }
}
